package a66weding.com.jiehuntong.widgets.horizontal;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.os.SystemClock;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.xutils.common.Callback;
import org.xutils.common.util.DensityUtil;
import org.xutils.x;

import java.io.ByteArrayOutputStream;
import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.ShopDetailsActivity;
import a66weding.com.jiehuntong.adapter.MyBaseAdapter;
import a66weding.com.jiehuntong.beans.GoodShopBean;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;

public class HorizontalListViewAdapter extends MyBaseAdapter {
    private Context mContext;
    private int imgWidth= DensityUtil.dip2px(70);
    private int imgHeight=DensityUtil.dip2px(70);

    public HorizontalListViewAdapter(Context context, List data) {

        super(context, data);
        this.mContext = context;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_horizontal_view, null);
            viewHolder.iv_toplable1 = (ImageView) convertView.findViewById(R.id.iv_toplable1);
            viewHolder.tv_shopName1 = (TextView) convertView.findViewById(R.id.tv_shopName1);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final GoodShopBean.DataBean dataBean = (GoodShopBean.DataBean) data.get(position);

        x.image().loadDrawable(dataBean.getNvc_Logo(), ImageOptionsUtil.getcircle_fixy(), new Callback.CommonCallback<Drawable>() {

            @Override
            public void onSuccess(Drawable result) {
                if(result!=null) {
                    byte[] bytes = drawableToByte(result);
                    viewHolder.iv_toplable1.setImageBitmap(decodeSampledBitmapFromNet(bytes, imgWidth, imgHeight));
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });


        viewHolder.tv_shopName1.setText(dataBean.getNvc_StoreName());

        viewHolder.iv_toplable1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext, ShopDetailsActivity.class);
                intent.putExtra("nvc_num", dataBean.getNvc_No());
                intent.putExtra("BusinessID", dataBean.getI_Id());
                mContext.startActivity(intent);
            }
        });


        return convertView;
    }

    public class ViewHolder {
        ImageView iv_toplable1;
        TextView tv_shopName1;
    }



    public  synchronized  byte[] drawableToByte(Drawable drawable) {

        if (drawable != null) {
            Bitmap bitmap = Bitmap
                    .createBitmap(
                            drawable.getIntrinsicWidth(),
                            drawable.getIntrinsicHeight(),
                            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                                    : Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight());
            drawable.draw(canvas);
            int size = bitmap.getWidth() * bitmap.getHeight() * 4;
            // 创建一个字节数组输出流,流的大小为size
            ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
            // 设置位图的压缩格式，质量为100%，并放入字节数组输出流中
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            // 将字节数组输出流转化为字节数组byte[]
            byte[] imagedata = baos.toByteArray();
            return imagedata;
        }
        return null;
    }


    public static Bitmap decodeSampledBitmapFromNet(byte[] data, int reqWidth, int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        BitmapFactory.decodeByteArray(data, 0, data.length, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeByteArray(data, 0, data.length, options);
    }



    public static synchronized int calculateInSampleSize(BitmapFactory.Options options,
                                                         int reqWidth, int reqHeight) {

        options.inJustDecodeBounds = true;
        final int height = options.outHeight;
        final int width = options.outWidth;

        int inSampleSize = 1;
        //方式一需要的参数
        int inSampleSize1 = 1;

        if (height > reqHeight || width > reqWidth) {

            //方式一
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            while (((halfHeight / inSampleSize1) > reqHeight) && ((halfWidth / inSampleSize1) > reqWidth)) {
                SystemClock.sleep(2000);

                inSampleSize1 *= 2;
            }

        }
        return inSampleSize1;
    }


}